//
//  AnnulusView.m
//  demo
//
//  Created by make on 2017/4/20.
//  Copyright © 2017年 make. All rights reserved.
//

#import "AnnulusView.h"

const CGFloat pathWidth = 5;

@interface AnnulusView()
{
    //路径
    UIBezierPath *progressPath;
    //填充层
    
    //title
   // UILabel *titleLabel;
}
/** fill View **/
@property(nonatomic,strong) CAShapeLayer *progressLayer;


/** titleLabel **/
@property(nonatomic,strong) UILabel *titleLabel;

@end
@implementation AnnulusView
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
            
            [self draw];

    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
            [self draw];
            

    }
    return self;
}

- (void)draw{

    
    
    UIBezierPath *bottomPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(px1080Width(600)/2, px1080Width(600)/2)
                                                              radius:px1080Width(600)/2
                                                          startAngle:0
                                                            endAngle:M_PI*2
                                                           clockwise:YES];
    bottomPath.lineWidth = pathWidth;
    
    CAShapeLayer *bottomLayer = [CAShapeLayer layer];
    bottomLayer.path = bottomPath.CGPath;
    bottomLayer.lineWidth = pathWidth;
    bottomLayer.strokeColor = [UIColor colorWithHexString:@"DEDEDE"].CGColor;
    bottomLayer.fillColor = nil;
    [self.layer addSublayer:bottomLayer];
    
    
    
    progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(px1080Width(600)/2, px1080Width(600)/2)
                                                  radius:px1080Width(600)/2
                                              startAngle:M_PI*3/2
                                                endAngle:M_PI*3/2 + 2*M_PI
                                               clockwise:YES];
    
    
    CABasicAnimation *annimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    annimation.fromValue = @0;
    annimation.toValue = @0;
    annimation.duration = 1;
    [self.progressLayer addAnimation:annimation forKey:nil];
    
}

- (UILabel *)titleLabel{
    
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
        _titleLabel.textColor = [UIColor colorWithRed:95/255.0 green:166/255.0 blue:135/255.0 alpha:1];
        _titleLabel.font = [UIFont systemFontOfSize:50];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.center = CGPointMake(px1080Width(600)/2, px1080Width(600)/2);
        [self addSubview:_titleLabel];
        
    }
    return _titleLabel;
}

- (CAShapeLayer *)progressLayer{
    
    if (!_progressLayer) {
        
        _progressLayer = [CAShapeLayer layer];
        _progressLayer.path = progressPath.CGPath;
        _progressLayer.lineWidth = pathWidth;
        _progressLayer.strokeColor = [UIColor colorWithRed:95/255.0 green:166/255.0 blue:135/255.0 alpha:1].CGColor;
        _progressLayer.fillColor = nil;
        [self.layer addSublayer:_progressLayer];
    }
    return _progressLayer;
}

- (void)setProgressValue:(CGFloat)progressValue andTitle:(NSString *)title{

    _progressValue = progressValue;
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:title];
    

    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:14.0]
     
                          range:NSMakeRange([AttributedStr length]-1, 1)];
    
    self.titleLabel.attributedText = AttributedStr;
    
    
    progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(px1080Width(600)/2, px1080Width(600)/2)
                                                  radius:px1080Width(600)/2
                                              startAngle:M_PI*3/2
                                                endAngle:M_PI*3/2 + (2*M_PI *progressValue)
                                               clockwise:YES];
  
      self.progressLayer.path = progressPath.CGPath;
    
    
  
    
    
}
@end
